"""
数据库模型对象定义
"""
from django.contrib.auth.base_user import BaseUserManager
from django.db import models
from django.contrib.auth.models import AbstractBaseUser

# Create your models here.
class Course(models.Model):
    """课程表"""
    course_no = models.CharField(
        max_length=50,
        verbose_name="课程编号",
        unique=True,
        primary_key=True,
        null=False
    )
    course_name = models.CharField(
        max_length=100,
        verbose_name="课程名称",
        null=False
    )
    course_hours = models.IntegerField(verbose_name="课程学时", null=False)
    type_id = models.IntegerField(verbose_name="课程类型", null=False)
    course_status = models.CharField(max_length=1, verbose_name="课程状态", null=False)
    course_reqs = models.CharField(max_length=20, null=False)
    course_point = models.DecimalField(
        max_digits=3,
        decimal_places=1,
        verbose_name="课程学分",
        null=True,
        default=0
    )
    course_memo = models.CharField(max_length=1000, null=True)
    course_textbook_pic = models.BinaryField(null=True, blank=True)

    class Meta:
        db_table = "tbl_course"  # 自定义表名

class CourseType(models.Model):
    """课程类型表"""
    type_id = models.IntegerField(verbose_name="课程类型编号", primary_key=True)
    type_name = models.CharField(max_length=30, verbose_name="课程类型名称", null=False)

    class Meta:
        db_table = "tbl_course_type"  # 自定义表名

class UserManager(BaseUserManager):
    """Custom manager for the User model."""
    def get_by_natural_key(self, user_no):
        """
        Find a user by their natural key (user_no).
        """
        return self.get(user_no=user_no)

class User(AbstractBaseUser):
    """用户表"""
    user_no = models.CharField(max_length=20, verbose_name="用户账号", null=False, primary_key=True)
    user_pwd = models.CharField(max_length=1000, verbose_name="用户密码", null=False)
    user_name = models.CharField(max_length=100, verbose_name="用户名", null=False)

    password = None

    # 指定登录时使用的字段
    USERNAME_FIELD = 'user_no'  # 用于认证的唯一标识字段
    REQUIRED_FIELDS = ['user_name']  # 创建超级用户时需要的其他字段

    objects = UserManager()

    class Meta:
        db_table = "tbl_user"  # 自定义表名

    def check_password(self, password):
        """
        检查密码是否正确
        ----
        Args:
            password (str): 密码
        Returns:
            bool: 密码是否正确
        """
        return self.user_pwd == password
